package com.kylin.upms.biz.web;


import com.kylin.upms.biz.annotation.Log;
import com.kylin.upms.biz.dto.MenuRoleDto;
import com.kylin.upms.biz.entity.Menu;
import com.kylin.upms.biz.entity.MenuRole;
import com.kylin.upms.biz.entity.UserSecurity;
import com.kylin.upms.biz.service.IMenuRoleService;
import com.kylin.upms.biz.service.IMenuService;
import com.kylin.upms.biz.utils.UserUtils;
import com.kylin.common.result.ResEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2019-04-11
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    IMenuService menuService;

    @Autowired
    IMenuRoleService iMenuRoleService;

    @Log("根据当前登录人的id查到权限菜单")
    @RequestMapping(value = "/getMenuByUserID",method = RequestMethod.GET)
    public ResEntity getMenuByUserID(){

        logger.info(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        UserSecurity userSecurity = UserUtils.getUser();
        List<Menu> menuList = menuService.getMenuByUserID(userSecurity.getUser().getId());
        return ResEntity.ok(menuList);
    }


    @RequestMapping(value = "/list/all",method = RequestMethod.GET)
    public ResEntity getMenuListAll(){
        List<Menu> menuListAll = menuService.getMenuListAll();
        return  ResEntity.ok(menuListAll);
    }


    @RequestMapping(value = "/role",method = RequestMethod.POST)
    public ResEntity saveMenuRole(@RequestBody  MenuRoleDto menuRoleDto){
        List<MenuRole> menuRoles = new ArrayList<>();
        for (Integer mid : menuRoleDto.getMids()) {
            MenuRole menuRole = new MenuRole();
            menuRole.setRid(menuRoleDto.getRid());
            menuRole.setMid(mid);
            menuRoles.add(menuRole);
        }
        iMenuRoleService.save(menuRoles);
        return ResEntity.ok();
    }
}
